package logic

import (
	"context"
	"errors"
	"transaction/moder/mysql"

	"transaction/tranrpc/internal/svc"
	"transaction/tranrpc/tranrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type UserAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUserAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserAddLogic {
	return &UserAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UserAddLogic) UserAdd(in *tranrpc.UserAddRequest) (*tranrpc.UserAddResponse, error) {
	// todo: add your logic here and delete this line
	var user mysql.User
	err := user.UserName(mysql.DB, in.Password)
	if err == nil {
		return nil, errors.New("商品已存在")
	}
	user = mysql.User{
		Username: in.Username,
		Password: in.Password,
	}
	err = user.UserAdd(mysql.DB)
	if err != nil {
		return nil, err
	}

	return &tranrpc.UserAddResponse{
		UserId: int64(user.ID),
	}, nil

}
